package com.aiit.mapper;

import com.aiit.annotation.AutoFill;
import com.aiit.dto.UserBookingDTO;
import com.aiit.entity.Booking;
import com.aiit.entity.UserBooking;
import com.aiit.enumeration.OperationType;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface BookingMapper {


    @Update("UPDATE bookings SET status = #{status} WHERE DATE_FORMAT(session_time, '%Y-%m-%d %H') = DATE_FORMAT(#{sessionTime}, '%Y-%m-%d %H')")
    void update(Booking booking);

    List<Booking> list(Booking booking);

    @Update("UPDATE bookings SET status = 3 WHERE session_time <  #{sessionTime}")
    int updateBookingsBySessionTime(LocalDateTime sessionTime);

    @Insert("insert into userbooking(username,status,session_time,create_time,update_time,admin_status)" +
            "values" +
            "(#{username},#{status},#{sessionTime},#{createTime},#{updateTime},#{admin_status})")
    @AutoFill(value = OperationType.INSERT)
    void insertUserBooking(UserBooking userBooking);

    @Update("UPDATE bookings SET seat_number = seat_number - 1 WHERE DATE_FORMAT(session_time, '%Y-%m-%d %H') = DATE_FORMAT(#{session_time}, '%Y-%m-%d %H')")
    void reduce(LocalDateTime session_time);

    @Update("UPDATE userbooking SET status = 3 WHERE session_time <  #{sessionTime}")
    int updateUserBookingBySessionTime(LocalDateTime fiveMinutesAgo);

    @Update("UPDATE bookings SET status = 0 WHERE seat_number= 0")
    int updateUserBookingStatus();

    List<Booking> select(String date);

    @Select("select * from userbooking where username = #{username} and status = 1 and session_time is not null")
    Page<UserBookingDTO> getByUserBooking(UserBookingDTO userBookingDTO);

//    @AutoFill(value = OperationType.UPDATE)
//    void updateUserBooking(Booking booking);
}
